package com.cx.base.day13_io.io_02_fileStream;

import java.io.*;

/**
 * @Author: robert.song
 * @Date: Created in 2022/4/22
 */
public class FileInput2OutStreamTest {
    public static void main(String[] args) {
//        基于字节的读写
//        baseByte();
//        基于字节数组的读写
        int i1 = 6;
        final boolean b1 = true;
        final int i2 = 1;
        System.out.println(i1 + "," + i2);
        baseByteArray();
    }

    private static void baseByte() {
        FileInputStream fileInputStream = null;
        FileOutputStream fileOutputStream = null;
        long start = 0;
        try {
            String separator = System.getProperty("file.separator");
//            文件在根项目下
            File file1 = new File("." + separator + "test0.txt");
            File file2 = new File("." + separator + "test1.txt");
            if (!file1.exists()) {
                if (!file1.getParentFile().exists()) {
                    file1.getParentFile().mkdirs();
                }
                file1.createNewFile();
            }
            if (!file2.exists()) {
                if (!file2.getParentFile().exists()) {
                    file2.getParentFile().mkdirs();
                }
                file2.createNewFile();
            }
            fileInputStream = new FileInputStream(file1);
            fileOutputStream = new FileOutputStream(file2, true);
            boolean flag = true;
            int asciiOfByte = 0;
            start = System.currentTimeMillis();
            while (flag) {
                // 读取数据，一次只读一个字节
                asciiOfByte = fileInputStream.read();
                if (asciiOfByte == -1) {
                    flag = false;
                    break;
                }
                // 底层走的是Buffer缓存，缓存满了刷新磁盘，即判断this.buffer.hasRemaining()为false执行刷盘
                fileOutputStream.write(asciiOfByte);
                // 要想即可刷新磁盘，执行flush()
                fileOutputStream.flush();
            }
            System.out.println(System.currentTimeMillis() - start);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (fileInputStream != null) {
                    fileInputStream.close();
                }
                if (fileOutputStream != null) {
                    fileOutputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static void baseByteArray() {
        FileInputStream fileInputStream = null;
        FileOutputStream fileOutputStream = null;
        long start = 0;
        try {
            String separator = System.getProperty("file.separator");
//            文件在根项目下
            File file1 = new File("." + separator + "test0.txt");
            File file2 = new File("." + separator + "test2.txt");
            file1.createNewFile();
            file2.createNewFile();
            fileInputStream = new FileInputStream(file1);
            fileOutputStream = new FileOutputStream(file2, true);
            byte[] bytes = new byte[8192];
            boolean flag = true;
            int byteCount = 0;
            start = System.currentTimeMillis();
            while (flag) {
                //        读取数据
                byteCount = fileInputStream.read(bytes);
                if (byteCount == -1) {
                    flag = false;
                    break;
                }
                //        写数据
                fileOutputStream.write(bytes, 0, byteCount);
                //            write会自动适时刷新会磁盘，要想直接刷新执行flush()
                fileOutputStream.flush();
            }
            System.out.println(System.currentTimeMillis() - start);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (fileInputStream != null) {
                    fileInputStream.close();
                }
                if (fileOutputStream != null) {
                    fileOutputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
